package Service;

import Data.MassageData;
import Data.event;
import MyInterfaces.MassageType;
import javafx.application.Platform;
import javafx.scene.control.Alert;

import java.io.*;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class PutFile {
    private event massage;

    public PutFile(event massage) {
        this.massage = massage;
    }

    public void putFileToMe() throws IOException, NoSuchAlgorithmException {
        System.out.println(massage.getType().equals(MassageType.Return_Get_File_Success));

        if(massage.getType().equals(MassageType.Return_Get_File_Success))
        {
            MassageData massageData=(MassageData) massage.getT();
            //文件返回成功默认返回到目录
            String path="F:\\qqclientfile";
            path=path+"\\"+massageData.getFile_name();

            File file=new File(path);
            if(file.exists())
            {
                String finalPath = path;
                Platform.runLater(() -> {
                    Alert alert = new Alert(Alert.AlertType.INFORMATION);
                    alert.setTitle("成功");
                    alert.setHeaderText(null);
                    alert.setContentText("你读取的"+massageData.getFile_name()+"文件已经在磁盘目录"+ finalPath +"下!");
                    alert.showAndWait();
                });
                return;
            }

            FileOutputStream fileOutputStream=new FileOutputStream(file);
            fileOutputStream.write(massageData.getBytes());
            fileOutputStream.close();

            if(!getMD5(file).equals(massageData.getMD5()))
            {
                System.out.println("MD5值不一样");
                file.delete();
                Platform.runLater(() -> {
                    Alert alert = new Alert(Alert.AlertType.INFORMATION);
                    alert.setTitle("失败");
                    alert.setHeaderText(null);
                    alert.setContentText("读取"+massageData.getFile_name()+"的文件失败!");
                    alert.showAndWait();
                });
                return ;
            }
            String finalPath1 = path;
            Platform.runLater(() -> {
                Alert alert = new Alert(Alert.AlertType.INFORMATION);
                alert.setTitle("成功");
                alert.setHeaderText(null);
                alert.setContentText("读取"+massageData.getFile_name()+"的文件成功!"+"存储到了"+ finalPath1 +"目录下面");
                alert.showAndWait();
            });
        }
        else
        {
            Platform.runLater(() -> {
                MassageData massageData=(MassageData) massage.getT();
                Alert alert = new Alert(Alert.AlertType.INFORMATION);
                alert.setTitle("失败");
                alert.setHeaderText(null);
                alert.setContentText("读取"+massageData.getFile_name()+"的文件失败!");
                alert.showAndWait();
          });
        }
        return ;
    }

    public static String getMD5(File file) throws IOException, NoSuchAlgorithmException
    {
        InputStream inputStream = new FileInputStream(file);
        byte[] buffer = new byte[1024];
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        int numRead = 0;
        while ((numRead = inputStream.read(buffer)) > 0) {
            md5.update(buffer, 0, numRead);
        }
        inputStream.close();
        byte[] md5Bytes = md5.digest();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < md5Bytes.length; i++) {
            if (Integer.toHexString(0xFF & md5Bytes[i]).length() == 1) {
                sb.append("0").append(Integer.toHexString(0xFF & md5Bytes[i]));
            } else {
                sb.append(Integer.toHexString(0xFF & md5Bytes[i]));
            }
        }
        return sb.toString();
    }
}
